//
// Created by hjie on 23-5-10.
//
#include "loop_event.hpp"
#include "udp_socket.h"
#include "udp_server.h"
#include "define.h"
#include <sstream>
#include "connection.h"
#include "server.h"
#include "loop_event_define.h"


int main(int argc, const char * args[])
{
    LoopEvent loop_event;
    Server server(&loop_event);
    server.SetOnMessageFunc([](Connection * connection, const uint8_t * data, int length){

        printf("recv data : %s.\n", data);
        std::stringstream ss;
        ss << "HTTP/1.1 200 OK" << "\r\n";
        ss << "Server: openresty" << "\r\n";
        ss << "Date: Fri, 09 Feb 2024 05:24:22 GMT" << "\r\n";
        ss << "Content-Type: text/html" << "\r\n";
        ss << "\r\n";
        ss << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
              "<message module=\"SCSP\" version=\"1.1\">"
              "<header action=\"RESPONSE\" command=\"WEBTVCONTROL\"/> "
              "<body>"
              "<webTVControl result=\"0\" resultDesc=\"请到营业厅开通。错误代码：901000\" openMode=\"0\"> "
              "<copyRightId><![CDATA[]]></copyRightId>"
              "<copyRightName><![CDATA[]]></copyRightName>"
              "<copyRightProductInfo><![CDATA[]]></copyRightProductInfo>"
              "</webTVControl>"
              "</body>"
              "</message>" << "\r\n";
        connection->SendData(ss.str());
    });
    Server server_of_http(&loop_event);
    server_of_http.SetOnMessageFunc([](Connection * connection, const uint8_t * data, int length){

        printf("recv data : %s.\n", data);
        std::string content = "HTTP/1.1 200 OK\r\nServer: openresty\r\nDate: Fri, 09 Feb 2024 07:18:26 GMT\r\nContent-Type: text/html\r\nConnection: keep-alive";
        std::stringstream ss;
        ss << content << "\r\n\r\n";
        ss << R"(<?xml version="1.0" encoding="UTF-8"?><DeviceAuthResp><RespCode>0</RespCode></DeviceAuthResp>)"  << "\r\n";
        const std::string request = (const char *)data;

        connection->SendData(ss.str());
    });
    server_of_http.Listen(80);
    server.Listen(8095);
    loop_event.Run();
    return EXIT_SUCCESS;
}